package com.qf.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.qf.annotation.RequiresPermissionsDesc;
import com.qf.pojo.DtsAdmin;
import com.qf.service.AdminService;
import com.qf.util.ResponseUtil;
import com.sun.media.jfxmedia.logging.Logger;
import lombok.extern.log4j.Log4j;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/admin/admin")
@CrossOrigin("http://localhost")
@Slf4j
public class DtsAdminAdminController {
    @Autowired
    AdminService adminService;
    @RequiresPermissions("admin:admin:delete")
    @RequiresPermissionsDesc(menu = { "系统管理", "管理员管理" }, button = "删除")
    @PostMapping("/delete")
    public Object delete(@RequestBody DtsAdmin dtsAdmin){
        adminService.deleteById(dtsAdmin.getId());
        log.info("删除成功");
        return ResponseUtil.ok();
    }
    @RequiresPermissions("admin:admin:update")
    @RequiresPermissionsDesc(menu = { "系统管理", "管理员管理" }, button = "编辑")
    @PostMapping("/update")
    public Object update(@RequestBody DtsAdmin dtsAdmin){
        DtsAdmin dtsAdmin1=adminService.update(dtsAdmin);
        log.info("修改成功");
        return ResponseUtil.ok(dtsAdmin1);
    }
    @RequiresPermissions("admin:admin:create")
    @RequiresPermissionsDesc(menu = { "系统管理", "管理员管理" }, button = "添加")
    @GetMapping("/create")
    public Object insert(@RequestBody DtsAdmin dtsAdmin){
        DtsAdmin dtsAdmin1=adminService.insert(dtsAdmin);
        log.info("新增成功");
        return ResponseUtil.ok(dtsAdmin1);
    }
    @RequiresPermissions("admin:admin:list")
    @RequiresPermissionsDesc(menu = { "系统管理", "管理员管理" }, button = "查询")
    @GetMapping("/list")
    public Object list(
            @RequestParam(value="page",required = false,defaultValue = "1")Integer page,
            @RequestParam(value = "limit",required = false,defaultValue = "10")Integer limit,
            String username,String sort,String order){
            IPage<DtsAdmin> ipage=adminService.selectByPage(page,limit,username,sort,order);
            Map<String,Object> data=new HashMap<>();
            data.put("total",ipage.getTotal());
            data.put("items",ipage.getRecords());
            return ResponseUtil.ok(data);

    }
}
